#include "CodeType.h"

CodeType::CodeType() {
    replaceWords.push_back(std::string(";"));
    replaceWords.push_back(std::string(":"));
    replaceWords.push_back(std::string("."));
    replaceWords.push_back(std::string(","));
    replaceWords.push_back(std::string("("));
    replaceWords.push_back(std::string(")"));
    replaceWords.push_back(std::string("_"));
    replaceWords.push_back(std::string("-"));
    replaceWords.push_back(std::string("->"));
    replaceWords.push_back(std::string("["));
    replaceWords.push_back(std::string("/*"));
    replaceWords.push_back(std::string("*/"));
    replaceWords.push_back(std::string("/"));
    replaceWords.push_back(std::string("\\"));
    replaceWords.push_back(std::string("\\\\"));
    replaceWords.push_back(std::string("["));
    replaceWords.push_back(std::string("]"));
    replaceWords.push_back(std::string("*"));
    replaceWords.push_back(std::string("#"));
    replaceWords.push_back(std::string("\""));
    replaceWords.push_back(std::string("{"));
    replaceWords.push_back(std::string("}"));
    replaceWords.push_back(std::string("<"));
    replaceWords.push_back(std::string(">"));
    replaceWords.push_back(std::string("="));
    replaceWords.push_back(std::string("+"));
    replaceWords.push_back(std::string("-"));
    replaceWords.push_back(std::string("&"));
    replaceWords.push_back(std::string("!"));
    replaceWords.push_back(std::string("~"));
}

void CodeType::parse(std::string path, Catalogues* catalog,EXTRACTOR_KeywordList* list) {
    std::set<std::string> values;
    std::vector<std::string> split;
    int (*pf)(int) = tolower;
    std::vector<std::string> listAux;
    std::vector<std::string>::iterator splitIte;
    std::set<std::string>::const_iterator ite;
    std::ifstream configFile;
    configFile.exceptions(std::ifstream::badbit);
    configFile.open(path.c_str(), std::ios::in);
    std::string line;
    values.insert(extractFileName(path));
    getline(configFile, line);
    while (!configFile.eof()) {
        if (!line.empty()) {
            values.insert(line);
            ite = values.begin();
            for (;ite != values.end();ite++) {
                std::string value = *ite;
                std::transform(value.begin(), value.end(), value.begin(), pf);
                replace(value);
                listAux = StaticUtils::split(value, " ");
                for(splitIte=listAux.begin();splitIte!=listAux.end();){
                    StaticUtils::trimo(*splitIte);
                    if(StaticUtils::isNumeric(*splitIte) || (*splitIte).empty()) {
                        splitIte=listAux.erase(splitIte);
                    } else {
                        splitIte++;
                    }
                }
                split.insert(split.end(), listAux.begin(), listAux.end());
                matchStopWords(split);
                listAux.clear();
            }
            if(!split.empty())
                catalog->agregarDocumento(path, split);
        }
        split.clear();
        listAux.clear();
        values.clear();
        line.clear();
        getline(configFile, line);
    }
    configFile.close();
}

CodeType::~CodeType(){
    
}

